<template>
  <div class="login-box">
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <h3>用户注册</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>

      <el-form-item label="验证码" prop="code">
        <el-input v-model="form.code" placeholder="请输入邮箱验证码"></el-input>
        <el-button type="info" @click="sendCode('form')">发送验证码</el-button>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="reg('form')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { log } from 'util'

export default {
  name: 'UserLogin',
  data() {
    return {
      form: {
        username: '',  // 用户名
        password: '',  // 密码
        email: '',  // 邮箱
        code: ''  // 验证码
      },
      // 验证规则
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    sendCode() {
      let vm = this;
      console.log("邮箱是:",vm.form.email);
      // 校验通过发送axios请求
      this.axios({
        url: `http://localhost:9090/email/send?email=${vm.form.email}`,
        method: 'GET',
      }).then(function (resp) {
        //登录成功，要向vuex中存放user对象
        if (resp.data.code == 0) {
          // 说明登录成功，路由到首页
          // vm.$message.success('登录成功');
          vm.$message({
            message: resp.data.msg,
            type: 'success'
          });
        } else {
          vm.$message({
            message: resp.data.msg,
            type: 'error'
          });
        }
      })
    },
    reg(formName) {
      // 回调函数中的valid参数 表示校验结果
      this.$refs[formName].validate((valid) => {
        let vm = this;
        // 判断表单校验结果
        if (valid) {
          // 校验通过发送axios请求
          this.axios({
            url: 'http://localhost:9090/user/register',
            method: 'POST',
            data: {
              userName: vm.form.username,
              passWord: vm.form.password,
              email: vm.form.email,
              code: vm.form.code
            },
          }).then(function (resp) {
            //登录成功，要向vuex中存放user对象
            if (resp.data.code == 200) {
              // 说明登录成功，路由到首页
              // vm.$message.success('登录成功');
              vm.$message({
                message: resp.data.msg,
                type: 'success'
              });
              // 把用户信息放入store中，然后就可以在其他组件中获取用户信息了
              setTimeout(function () {
                vm.$router.push("/")
              }, 1000);
            } else {
              vm.$message({
                message: resp.data.msg,
                type: 'error'
              });
            }
          })
        } else {
          // 校验失败
          this.$message.error('输入格式错误');
          return false;
        }
      });
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login-box {
  width: 500px;
  height: 400px;
  border: 1px solid #DCDFE6;
  margin: 150px auto;
  padding: 20px 50px 20px 30px;
  border-radius: 20px;
  box-shadow: 0px 0px 20px #DCDFE6;
}
</style>
